package com.lewin.ringroad.net.message;

import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 排队统计数据（0x52）
 *
 * @author Jun
 * @since 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class QueueStatisticMessage extends BaseMessage {
    //@formatter:off

    /** 检测通道数 */
    private short checkChannelNumber;

    /** 报文接收时间 */
    private long receivedTime = System.currentTimeMillis();

    /** 通道排队统计数据 */
    private List<ChannelStatisticData> channelDataList;

    //@formatter:on


    @Override
    public BaseMessage parse(ByteBuf buf) {
        this.checkChannelNumber = buf.readUnsignedByte();
        this.channelDataList = new ArrayList<>(this.checkChannelNumber);
        for(int i = 0; i < this.checkChannelNumber; i++) {
            this.channelDataList.add(ChannelStatisticData.parseFrom(buf));
        }

        return this;
    }

    @Data
    public static class ChannelStatisticData {

        /** 检测通道号 */
        private short code;
        /** 排队长度，单位 m */
        private float queueLength;
        /** 通道排队数量 */
        private short queueNumber;
        /** 停车次数 */
        private short parkingCount;
        /** 停车时间 */
        private short parkingTime;

        public static ChannelStatisticData parseFrom(ByteBuf buf){
            ChannelStatisticData data = new ChannelStatisticData();
            data.code = buf.readUnsignedByte();
            data.queueLength = buf.readFloatLE();
            data.queueNumber = buf.readUnsignedByte();
            data.parkingCount = buf.readUnsignedByte();
            data.parkingTime = buf.readUnsignedByte();

            return data;
        }
    }
}
